#include "conf.h"

// from sx10
void change_video_tables(int a, int b){
}

// from sx10, not sure if the values are right, seems to work
void  set_quality(int *x){ // -17 highest; +12 lowest
 if (conf.video_mode) *x=12-((conf.video_quality-1)*(12+17)/(99-1));
}

void __attribute__((naked,noinline)) movie_record_task(){ 
 asm volatile(
"                STMFD   SP!, {R2-R8,LR}\n"
"                LDR     R8, =0x442\n"
"                LDR     R7, =0x2710\n"
"                LDR     R4, =0x5FA8\n"
"                MOV     R6, #0\n"
"                MOV     R5, #1\n"
"loc_FF92F5E0:\n"
"                LDR     R0, [R4,#0x18]\n"
"                MOV     R2, #0\n"
"                ADD     R1, SP, #4\n"
"                BL      sub_FF826C30\n" // KerQueue.c 0
"                LDR     R0, [R4,#0x20]\n"
"                CMP     R0, #0\n"
"                BNE     loc_FF92F6B0\n"
"                LDR     R0, [SP,#4]\n"
"                LDR     R1, [R0]\n"
"                SUB     R1, R1, #2\n"
"                CMP     R1, #9\n"
"                ADDLS   PC, PC, R1,LSL#2\n"
"                B       loc_FF92F6B0\n"
"                B       loc_FF92F664\n"
"                B       loc_FF92F684\n"
"                B       loc_FF92F694\n"
"                B       loc_FF92F69C\n"
"                B       loc_FF92F66C\n"
"                B       loc_FF92F6A4\n"
"                B       loc_FF92F674\n"
"                B       loc_FF92F6B0\n"
"                B       loc_FF92F6AC\n"
"                B       loc_FF92F63C\n"
"loc_FF92F63C:\n"
// jumptable FF92F60C entry 9
"                LDR     R0, =0xFF92F2F0\n" // locret
"                STR     R6, [R4,#0x34]\n"
"                STR     R0, [R4,#0x9C]\n"
"                LDR     R0, =0xFF92E96C\n"
"                LDR     R2, =0xFF92E888\n"
"                LDR     R1, =0x4B268\n"
"                STR     R6, [R4,#0x24]\n"
"                BL      sub_FF839668\n"
"                STR     R5, [R4,#0x38]\n"
"                B       loc_FF92F6B0\n"
"loc_FF92F664:\n"
// jumptable FF92F60C entry 0
"                BL      unlock_optical_zoom\n" // +
"                BL      sub_FF92F3E8\n" // MovieRecorder.c 0
"                B       loc_FF92F6B0\n"
"loc_FF92F66C:\n"
// jumptable FF92F60C entry 4
"                BL      sub_FF92EF94_my\n" // -> quality override
"                B       loc_FF92F6B0\n"
"loc_FF92F674:\n"
// jumptable FF92F60C entry 6
"                LDR     R1, [R0,#0x18]\n"
"                LDR     R0, [R0,#4]\n"
"                BL      sub_FFA42458\n" // MovWriter.c 0
"                B       loc_FF92F6B0\n"
"loc_FF92F684:\n"
// jumptable FF92F60C entry 1
"                LDR     R0, [R4,#0x38]\n"
"                CMP     R0, #5\n"
"                STRNE   R5, [R4,#0x28]\n"
"                B       loc_FF92F6B0\n"
"loc_FF92F694:\n"
// jumptable FF92F60C entry 2
"                BL      sub_FF92ECCC\n" // MovieRecorder.c 0
"                B       loc_FF92F6B0\n"
"loc_FF92F69C:\n"
// jumptable FF92F60C entry 3
"                BL      sub_FF92E9B8\n" // MovieRecorder.c 0
"                B       loc_FF92F6B0\n"
"loc_FF92F6A4:\n"
// jumptable FF92F60C entry 5
"                BL      sub_FF92E814\n" // MovieRecorder.c 100
"                B       loc_FF92F6B0\n"
"loc_FF92F6AC:\n"
// jumptable FF92F60C entry 8
"                BL      sub_FF92F818\n" // MovieRecorder.c 100
"loc_FF92F6B0:\n"
// jumptable FF92F60C default entry
// jumptable FF92F60C entry 7
"                LDR     R1, [SP,#4]\n"
"                LDR     R3, =0xFF92E678\n" // "MovieRecorder.c"
"                STR     R6, [R1]\n"
"                STR     R8, [SP]\n"
"                LDR     R0, [R4,#0x1C]\n"
"                MOV     R2, R7\n"
"                BL      sub_FF827584\n"
"                B       loc_FF92F5E0\n"
 );
}

void __attribute__((naked,noinline)) sub_FF92EF94_my(){ 
 asm volatile(
"                STMFD   SP!, {R4-R9,LR}\n"
"                SUB     SP, SP, #0x44\n"
"                MOV     R7, #0\n"
"                LDR     R6, =0x5FA8\n"
"                MOV     R4, R0\n"
"                STR     R7, [SP,#0x34]\n"
"                STR     R7, [SP,#0x2C]\n"
"                LDR     R0, [R6,#0x38]\n"
"                MOV     R8, #4\n"
"                CMP     R0, #3\n"
"                STREQ   R8, [R6,#0x38]\n"
"                LDR     R0, [R6,#0x9C]\n"
"                BLX     R0\n"
"                LDR     R0, [R6,#0x38]\n"
"                CMP     R0, #4\n"
"                BNE     loc_FF92F0E4\n"
"                ADD     R3, SP, #0x2C\n"
"                ADD     R2, SP, #0x30\n"
"                ADD     R1, SP, #0x34\n"
"                ADD     R0, SP, #0x38\n"
"                BL      sub_FFA424F4\n" // MovWriter.c 0
"                CMP     R0, #0\n"
"                MOV     R5, #1\n"
"                BNE     loc_FF92F010\n"
"                LDR     R1, [R6,#0x28]\n"
"                CMP     R1, #1\n"
"                BNE     loc_FF92F0F8\n"
"                LDR     R1, [R6,#0x4C]\n"
"                LDR     R2, [R6,#0x3C]\n"
"                CMP     R1, R2\n"
"                BCC     loc_FF92F0F8\n"
"loc_FF92F010:\n"
"                CMP     R0, #0x80000001\n"
"                STREQ   R8, [R6,#0x50]\n"
"                BEQ     loc_FF92F048\n"
"                CMP     R0, #0x80000003\n"
"                STREQ   R5, [R6,#0x50]\n"
"                BEQ     loc_FF92F048\n"
"                CMP     R0, #0x80000005\n"
"                MOVEQ   R0, #2\n"
"                BEQ     loc_FF92F044\n"
"                CMP     R0, #0x80000007\n"
"                STRNE   R7, [R6,#0x50]\n"
"                BNE     loc_FF92F048\n"
"                MOV     R0, #3\n"
"loc_FF92F044:\n"
"                STR     R0, [R6,#0x50]\n"
"loc_FF92F048:\n"
"                LDR     R0, =0x4B29C\n"
"                LDR     R0, [R0,#8]\n"
"                CMP     R0, #0\n"
"                BEQ     loc_FF92F060\n"
"                BL      sub_FF8486D8\n"
"                B       loc_FF92F064\n"
"loc_FF92F060:\n"
"                BL      sub_FF92E814\n" // MovieRecorder.c 100
"loc_FF92F064:\n"
"                LDR     R0, [R4,#0x1C]\n"
"                LDR     R1, [R4,#0x20]\n"
"                ADD     R3, SP, #0x3C\n"
"                MVN     R2, #1\n"
"                ADD     R7, SP, #0x18\n"
"                STMIA   R7, {R0-R3}\n"
"                LDR     R2, [R6,#0x64]\n"
"                LDR     R3, [R6,#0x68]\n"
"                MOV     R0, #0\n"
"                ADD     R1, SP, #0x40\n"
"                ADD     R7, SP, #8\n"
"                STMIA   R7, {R0-R3}\n"
"                MOV     R3, #0\n"
"                MOV     R2, #0x40\n"
"                STRD    R2, [SP]\n"
"                LDMIB   R4, {R0,R1}\n"
"                LDR     R3, =0x4B2B4\n"
"                MOV     R2, R9\n"
"                BL      sub_FFA05D9C\n"
"                LDR     R0, [R6,#0x10]\n"
"                MOV     R1, #0x3E8\n"
"                BL      sub_FF82704C\n" // TakeSemaphore
"                CMP     R0, #9\n"
"                BEQ     loc_FF92F17C\n"
"                LDR     R0, [SP,#0x3C]\n"
"                CMP     R0, #0\n"
"                BNE     loc_FF92F198\n"
"                MOV     R0, #1\n"
"                BL      sub_FFA05E44\n"
"                BL      sub_FFA05E80\n"
"                MOV     R0, #5\n"
"                STR     R0, [R6,#0x38]\n"
"loc_FF92F0E4:\n"
"                ADD     SP, SP, #0x44\n"
"                LDMFD   SP!, {R4-R9,PC}\n"
"loc_FF92F0EC:\n"
"                BL      sub_FF877FFC\n" // HardwareDefect
"                MOV     R0, #1\n"
"                B       loc_FF92F238\n"
"loc_FF92F0F8:\n"
"                LDR     R12, [SP,#0x34]\n"
"                CMP     R12, #0\n"
"                BEQ     loc_FF92F298\n"
"                STR     R5, [R6,#0x2C]\n"
"                LDR     R0, [R6,#0x4C]\n"
"                LDR     R8, [R4,#0xC]\n"
"                CMP     R0, #0\n"
"                LDRNE   LR, [SP,#0x38]\n"
"                BNE     loc_FF92F1C4\n"
"                LDR     R0, [R4,#0x1C]\n"
"                LDR     R1, [R4,#0x20]\n"
"                ADD     R3, SP, #0x3C\n"
"                MVN     R2, #0\n"
"                ADD     R9, SP, #0x18\n"
"                STMIA   R9, {R0-R3}\n"
"                LDR     R0, [SP,#0x2C]\n"
"                LDR     R2, [R6,#0x64]\n"
"                LDR     R3, [R6,#0x68]\n"
"                ADD     R1, SP, #0x40\n"
"                ADD     R9, SP, #8\n"
"                STMIA   R9, {R0-R3}\n"
"                LDR     R3, [SP,#0x30]\n"
"                STR     R12, [SP]\n"
"                STR     R3, [SP,#4]\n"
"                LDMIB   R4, {R0,R1}\n"
"                LDR     R3, [SP,#0x38]\n"
"                MOV     R2, R8\n"
"                BL      sub_FFA05D9C\n"
"                LDR     R0, [R6,#0x10]\n"
"                MOV     R1, #0x3E8\n"
"                BL      sub_FF82704C\n" // TakeSemaphore
"                CMP     R0, #9\n"
"                BNE     loc_FF92F18C\n"
"loc_FF92F17C:\n"
"                BL      sub_FFA429C0\n"
"                MOV     R0, #0x90000\n"
"                STR     R5, [R6,#0x38]\n"
"                B       loc_FF92F0EC\n"
"loc_FF92F18C:\n"
"                LDR     R0, [SP,#0x3C]\n"
"                CMP     R0, #0\n"
"                BEQ     loc_FF92F1A8\n"
"loc_FF92F198:\n"
"                BL      sub_FFA429C0\n"
"                MOV     R0, #0xA0000\n"
"                STR     R5, [R6,#0x38]\n"
"                B       loc_FF92F0EC\n"
"loc_FF92F1A8:\n"
"                MOV     R0, #1\n"
"                BL      sub_FFA05E44\n"
"                LDR     R0, [SP,#0x40]\n"
"                LDR     R1, [SP,#0x38]\n"
"                ADD     LR, R1, R0\n"
"                LDR     R1, [SP,#0x34]\n"
"                SUB     R12, R1, R0\n"
"loc_FF92F1C4:\n"
"                LDR     R0, [R4,#0x1C]\n"
"                LDR     R2, [R6,#0x48]\n"
"                LDR     R1, [R4,#0x20]\n"
"                ADD     R3, SP, #0x3C\n"
"                ADD     R9, SP, #0x18\n"
"                STMIA   R9, {R0-R3}\n"
"                LDR     R0, [SP,#0x2C]\n"
"                LDR     R2, [R6,#0x64]\n"
"                LDR     R3, [R6,#0x68]\n"
"                ADD     R1, SP, #0x40\n"
"                ADD     R9, SP, #8\n"
"                STMIA   R9, {R0-R3}\n"
"                LDR     R3, [SP,#0x30]\n"
"                STR     R12, [SP]\n"
"                STR     R3, [SP,#4]\n"
"                LDMIB   R4, {R0,R1}\n"
"                MOV     R3, LR\n"
"                MOV     R2, R8\n"
"                BL      sub_FFA05D9C\n"
"                LDR     R0, [R6,#0x10]\n"
"                MOV     R1, #0x3E8\n"
"                BL      sub_FF82704C\n" // TakeSemaphore
"                CMP     R0, #9\n"
"                BNE     loc_FF92F240\n"
"                BL      sub_FFA429C0\n"
"                MOV     R0, #0x90000\n"
"                STR     R5, [R6,#0x38]\n"
"                BL      sub_FF877FFC\n" // HardwareDefect
"                MOV     R0, #0\n"
"loc_FF92F238:\n"
"                BL      sub_FFA05E44\n"
"                B       loc_FF92F0E4\n"
"loc_FF92F240:\n"
"                LDR     R0, [SP,#0x3C]\n"
"                CMP     R0, #0\n"
"                BEQ     loc_FF92F260\n"
"                BL      sub_FFA429C0\n"
"                MOV     R0, #0xA0000\n"
"                STR     R5, [R6,#0x38]\n"
"                BL      sub_FF877FFC\n" // HardwareDefect
"                B       loc_FF92F0E4\n"
"loc_FF92F260:\n"
"                MOV     R0, #0\n"
"                BL      sub_FFA05E44\n"
"                LDR     R0, [SP,#0x38]\n"
"                LDR     R1, [SP,#0x40]\n"
"                BL      sub_FFA4271C\n" // MovWriter.c 0
"                LDR     R0, [R6,#0x48]\n"
"                LDR     R3, =0x6010\n"
"                ADD     R1, R0, #1\n"
"                STR     R1, [R6,#0x48]\n"
"                STR     R3, [SP]\n"
"                LDR     R0, [SP,#0x40]\n"
"                SUB     R3, R3, #4\n"
"                MOV     R2, #0xF\n"
"                BL      sub_FFA40A14\n"
"                LDR     R0, =0x6010-4\n" // +
"                BL      set_quality\n" // +
"loc_FF92F298:\n"
"                LDR     R0, [R6,#0x4C]\n"
"                ADD     R0, R0, #1\n"
"                STR     R0, [R6,#0x4C]\n"
"                LDR     R1, [R6,#0x74]\n"
"                MUL     R0, R1, R0\n"
"                LDR     R1, [R6,#0x70]\n"
"                BL      sub_FFACE280\n"
"                MOV     R4, R0\n"
"                BL      sub_FFA429F8\n"
"                LDR     R1, [R6,#0x6C]\n"
"                CMP     R1, R4\n"
"                BNE     loc_FF92F2D4\n"
"                LDR     R0, [R6,#0x30]\n"
"                CMP     R0, #1\n"
"                BNE     loc_FF92F2E8\n"
"loc_FF92F2D4:\n"
"                LDR     R1, [R6,#0x80]\n"
"                MOV     R0, R4\n"
"                BLX     R1\n"
"                STR     R4, [R6,#0x6C]\n"
"                STR     R7, [R6,#0x30]\n"
"loc_FF92F2E8:\n"
"                STR     R7, [R6,#0x2C]\n"
"                B       loc_FF92F0E4\n"
 );
}
